package com.openstack4j.qx.rest;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

import com.openstack4j.qx.IPController;
import com.openstack4j.qx.imp.IPcontrollerImpl;
import com.openstack4j.qx.pojo.ApplyIPResp;
import com.openstack4j.qx.pojo.BaseResp;

/**
 * ClassName:IPRest
 * Function: ip相关操作rest定义实现. 
 * Create Date:  2016年12月5日
 * Author:       yuanzhijin
 * @see 	 
 */

@Path("/IPRest")
public class IPRest {
	
	
	 IPController  ipcontroller=new IPcontrollerImpl();
	
	/**
	 * 申请ip 
	 * 
	 * @param request
	 * @return
	 */
	@POST
	@Path("/ApplyIPReq")
	@Produces(MediaType.APPLICATION_JSON)
	public ApplyIPResp applyIpReq(@Context HttpServletRequest request){
		
		ApplyIPResp resp  =new ApplyIPResp();
		try {
			String ipType = request.getParameter("IpType");
			String appId = request.getParameter("AppID");
			String appName = request.getParameter("AppName");
			//参数是否不为空
			if(ipType!=null && appId !=null && appName != null){
				resp =ipcontroller.applyIp(ipType, appId, appName);
			}else{
				System.out.println("参数为空");
				resp.setFaultstring("parameters error!");
			}
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}	
	
	
	/**
	 * 释放ip 
	 * 
	 * @param request
	 * @return
	 */
	@POST
	@Path("/ReleaseIPReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp releaseIpReq(@Context HttpServletRequest request){
		
		BaseResp resp  =new BaseResp();
		try {
			String ip = request.getParameter("IP");
			if(ip!=null){
				resp =ipcontroller.releaseIp(ip);
			}else{
				resp.setFaultstring("parameters error!");
			}
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}	
	
	
	
	/**
	 * 绑定ip 
	 * 
	 * @param request
	 * @return
	 */
	@POST
	@Path("/BindingIPReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp bindingIpReq(@Context HttpServletRequest request){
		BaseResp resp  =new BaseResp();
		try {
			String ip = request.getParameter("IP");
			String resourceId = request.getParameter("ResourceId");
			
			
			
			//参数是否不为空
			if(ip!=null && resourceId !=null){
				resp =ipcontroller.bindIp(ip, resourceId);
			}else{
				resp.setFaultstring("parameters error!");
			}
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}	
	
	
	
	/**
	 * 解绑ip 
	 * 
	 * @param request
	 * @return
	 */
	@POST
	@Path("/UnbindingIPReq")
	@Produces(MediaType.APPLICATION_JSON)
	public BaseResp unbindingIpReq(@Context HttpServletRequest request){
		BaseResp resp  =new BaseResp();
		try {
			String ip = request.getParameter("IP");
			String resourceId = request.getParameter("ResourceId");
			
			//参数是否不为空
			if(ip!=null && resourceId !=null){
				resp =ipcontroller.unbindIp(ip, resourceId);
			}else{
				resp.setFaultstring("parameters error!");
			}
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}	
	
	
	
	

}

